import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const Layout = () => import('@/views/Layout.vue')
const Detail = () => import('@/views/Detail.vue')
const Article = () => import('@/views/Article.vue')
const Collect = () => import('@/views/Collect.vue')
const Like = () => import('@/views/Like.vue')
const User = () => import('@/views/User.vue')

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        { path: '/', redirect: '/article' },
        {
            path: '/', component: Layout, children: [
                { path: '/article', component: Article },
                { path: '/collect', component: Collect },
                { path: '/like', component: Like },
                { path: '/user', component: User }
            ]
        },
        { path: '/detail/:id', component: Detail },
        { path: '/login', component: Login },
        { path: '/register', component: Register }
    ]
})

const whitelist = ['/', '/article', '/login', '/register']
router.beforeEach((to, from, next) => {
    if (getToken() || whitelist.includes(to.path)) {
        next()
    } else {
        next('/login')
    }
})

export default router
